﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoServer.Helpers;

namespace VideoServer.ViewModel.Helpers
{
    public class SaveFlyInfoFile
    {
        private string fullFileName = null;
        private FileStream fs = null;
        private BinaryWriter bw = null;
        private bool isCreateDirectory = false;
        public string FullFileName
        {
            get { return fullFileName; }
        }

        public SaveFlyInfoFile()
        {
            string savePath = SystemSettingHelper.GetFileSaveSettingValue("FlyInfoSavePath");
            try
            {
                if (!Directory.Exists(@savePath))
                {
                    Directory.CreateDirectory(@savePath);
                    isCreateDirectory = true;
                }
                fullFileName = @savePath + "\\" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".bin";
            }
            catch (Exception ex)
            {
                isCreateDirectory = false;
                ErrorHelper.OnPrintLog(this, new CustomEventArgs() { MessageType = MsgTyp.ERROR, Message = "创建飞参存储文件夹错误" + ex.Message });
            }
            //fs = new FileStream(fullFileName, FileMode.OpenOrCreate);
            //bw = new BinaryWriter(fs);
        }

        //写数据
        public void WriteData(byte[] data)
        {
            //if(bw!=null)
            //bw.Write(data);
            if (isCreateDirectory)
            {
                using (fs = new FileStream(fullFileName, FileMode.Append))
                {
                    using (bw = new BinaryWriter(fs))
                    {
                        bw.Write(data);
                    }
                }
            }
        }
        //关闭文件流
        public void Close()
        {
            if (bw != null)
                bw.Close();
            if (fs != null)
                fs.Close();
        }
    }
}
